﻿using Microsoft.WindowsAzure.Storage.Table.DataServices;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.Reflection;
using Microsoft.WindowsAzure.Storage.Table;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;

namespace POC.Azure.Model
{

    [Serializable()]
    public class LogEntry : TableEntity
    {
        // By default, when creating a new entity, the PartitionKey is set to the current year, and the RowKey is a GUID. Insert the ticks in the beginning of RowKey because the result returned by a query is ordered by PartitionKey and then RowKey.
        public LogEntry()
        {
            this.Fill();
        }

        public LogEntry(string message, string category)
        {
            this.Fill();
            this.Message = message;
            this.Category = category;
        }

        public LogEntry(string source, TraceEventType eventType, int id, TraceEventCache eventCache, TraceOptions options, string message)
        {
            this.Fill();
            this.Message = message;
            this.Category = eventType.ToString();
            this.LogSource = source;
            this.EventID = id;
            this.EventType = (int)eventType; // INFO
            this.StackTrace = eventCache.Callstack;
            this.ThreadId = eventCache.ThreadId;
            this.ProcessId = eventCache.ProcessId;

            this.traceEventType = eventType;
            this.eventCache = eventCache;
            this.traceOutputOptions = options;

            this.Timestamp = (eventCache != null) ? eventCache.DateTime : DateTime.UtcNow;
        }



        public long LogID { get; set; }

        public string Category { get; set; }

        public short CategoryNumber { get; set; }

        public int EventType { get; set; }

        public int EventID { get; set; }

        public string Data { get; set; }

        public string Tags;

        public string InstanceID { get; set; }

        public string DeploymentID { get; set; }

        public string MachineName { get; set; }

        public int ProcessId { get; set; }

        public string ThreadId { get; set; }

        public string LogSource { get; set; }

        public string UserName { get; set; }

        public string Message { get; set; }

        public string StackTrace { get; set; }

        public long ErrorCode { get; set; }

        public System.DateTime TimeGenerated { get; set; }

        public System.DateTime TimeWritten { get; set; }

        public System.Guid CoRelationGuid { get; set; }


        [NonSerialized()]
        [XmlIgnore]
        internal TraceOptions traceOutputOptions;

        [NonSerialized()]
        [XmlIgnore]
        internal TraceEventCache eventCache;

        // ??
        [NonSerialized()]
        [XmlIgnore]
        internal TraceEventType traceEventType;

        public string ToString(string delimiter)
        {
            LogEntry message = this;
            string[] allProperties = new string[] { 
            message.Category, 
            message.CategoryNumber.ToString(), 
            message.CoRelationGuid.ToString(), 
            message.Data, 
            message.DeploymentID, 
            message.ErrorCode.ToString(),
            message.ETag, 
            message.eventCache == null ? string.Empty:  message.eventCache.ToString(),
            message.EventID.ToString(),
            message.EventType.ToString(), 
            message.InstanceID, 
            message.LogID.ToString(),
            message.LogSource, 
            message.MachineName, 
            message.Message, 
            message.PartitionKey, 
            message.ProcessId.ToString(), 
            message.RowKey, 
            message.Tags,
            message.ThreadId, 
            message.TimeGenerated.ToString(), 
            message.Timestamp.ToString(), 
            message.TimeWritten.ToString()
            };
            return string.Join(delimiter, allProperties);
        }
        public override string ToString()
        {
            string delimiter = "|";
            return ToString(delimiter);
        }
    }   
}
